package com.shixi.shixitraining.shixitraining.ui.tabapply.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.TabhostBaseFragment;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabapply.adapter.ApplyEmploymentAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabapply.entity.ApplyEmploymentBean;
import com.shixi.shixitraining.shixitraining.ui.tabapply.activity.EmploymenApplyDetailActivity;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.APPLYEMPLOY;

/**
 * Created by shenyan on 2018/7/17.
 */

public class ApplyEmploymentFragment extends TabhostBaseFragment {

    private TextView mDataTv;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_employment;
    }

    @Override
    protected void initPager() {
        initView();
    }

    private void initView() {

        mDataTv = (TextView) mContentView.findViewById(R.id.tv_nodata);

        String userId = UserManager.getInstance().getUserId();
        String token = UserManager.getInstance().getToken();

        initRequestEmployment(userId,token);

    }

    private void initRequestEmployment(String userId, String token) {
//        String url="http://api.shixigroup.com/myapply/job?usserId=3&token=033e191242ed1f4b503f9bd6fca6e2d4";

        String url=APPLYEMPLOY+"&userId="+userId+"&token="+token;
        HttpUtil.getInstance().getApiInterface().applyEmploy(url).enqueue(new CallbackImple<ApplyEmploymentBean>() {
            @Override
            public void onError(Call<ApplyEmploymentBean> call, Throwable t) {
                ToastUtil.showShortToast(getContext(),t.getMessage());
            }

            @Override
            public void onSuccess(Call<ApplyEmploymentBean> call, ApplyEmploymentBean response) {
                if (response.getStatus()==200){
                    if (response.getData()!=null&&response.getData().size()>0){
                        final List<ApplyEmploymentBean.DataBean> data = response.getData();
                        RecyclerView rcview_employment = (RecyclerView) mContentView.findViewById(R.id.rcview_employment);
                        LinearLayoutManager manager=new LinearLayoutManager(getContext());
                        rcview_employment.setLayoutManager(manager);
                        ApplyEmploymentAdapter  adapter=new ApplyEmploymentAdapter(getContext(),data);
                        rcview_employment.setAdapter(adapter);
                        rcview_employment.addOnItemTouchListener(new RecycleItemClickListener(getContext()) {
                            @Override
                            public void itemClickListener(View view, int position) {
                                ApplyEmploymentBean.DataBean dataBean = data.get(position);
                                String number = dataBean.getNumber();
                                Bundle bundle=new Bundle();
                                bundle.putString("number",number);
                                Intent intent=new Intent(getContext(),EmploymenApplyDetailActivity.class);
                                intent.putExtras(bundle);
                                 startActivity(intent);
                            }
                        });

                    }else {
                        ToastUtil.showShortToast(getContext(),response.getMsg());
                    }
                }else {
                    mDataTv.setVisibility(View.VISIBLE);
                    ToastUtil.showShortToast(getContext(),"数据为空");
                }
            }
        });
    }


}